package com.hq.controller;

import com.hq.entity.Exam;
import com.hq.entity.ResultVo;
import com.hq.service.ExamService;
import com.hq.service.QuestionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/exam")
@Slf4j
public class ExamCopntroller {

    @Autowired
    private ExamService examService;
    @Autowired
    private QuestionService questionService;


    //学生考试界面
    @RequestMapping("/online")
    public Map<String,Object> online(){
        return questionService.findOnline();
    }

    //查询所有试卷
    @RequestMapping("/all")
    public List<Exam> all(){
        return examService.findAll();
    }

    //分页查询
    @RequestMapping("/list")
    public Map<String,Object> list(int page,int size){
        return examService.findByPage(page,size);
    }

    //添加试卷
    @RequestMapping("/add")
    public ResultVo add(@RequestBody Exam exam){
        log.info("添加试卷：{}",exam);
        ResultVo vo=new ResultVo();
        if(examService.addExam(exam)>0){
            vo.setCode(200);
            vo.setMsg("添加成功！");
        }else{
            vo.setCode(500);
            vo.setMsg("添加失败！");
        }
        return vo;
    }

    //发布试卷
    @RequestMapping("/publish")
    public ResultVo publish(int eid){
        log.info("发布试卷：{}",eid);
        ResultVo vo=new ResultVo();
        if(examService.publishExam(eid)>0){
            vo.setCode(200);
            vo.setMsg("发布成功！");
        }else{
            vo.setCode(500);
            vo.setMsg("发布失败！");
        }
        return vo;
    }

    //删除试卷
    @RequestMapping("/del")
    public ResultVo del(int eid){
        log.info("删除试卷：{}",eid);
        ResultVo vo=new ResultVo();
        if(examService.delExam(eid)>0){
            vo.setCode(200);
            vo.setMsg("删除成功！");
        }else{
            vo.setCode(500);
            vo.setMsg("删除失败！");
        }
        return vo;
    }

    //批量删除试卷
    @RequestMapping("/dels")
    public ResultVo dels(String ids){
        log.info("批量删除的试卷id{}",ids);
        ResultVo vo=new ResultVo();
        if(examService.delExams(ids)>0){
            vo.setCode(200);
            vo.setMsg("删除成功！");
        }else{
            vo.setCode(500);
            vo.setMsg("删除失败！");
        }
        return vo;
    }

    //编辑试卷
    @RequestMapping("/edit")
    public ResultVo edit(@RequestBody Exam exam){
        log.info("修改的试卷{}",exam);
        ResultVo vo=new ResultVo();
        if(examService.editExam(exam)>0){
            vo.setCode(200);
            vo.setMsg("添加成功！");
        }else{
            vo.setCode(500);
            vo.setMsg("添加失败！");
        }
        return vo;
    }
}
